package common;

public class CryptMgr
{
	public static String CryptPassword(String Key, String Password)
    {
        char[] HASH = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
            't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'};

        String _Crypted = "";

        for (byte i = 0; i < Password.length(); i++)
        {
            char PPass = Password.charAt(i);
            char PKey = Key.charAt(i);

            int APass = (int)PPass / 16;

            byte AKey = (byte)((int)PPass % 16);

            byte ANB = (byte)((APass + (int)PKey) % HASH.length);
            byte ANB2 = (byte)((AKey + (int)PKey) % HASH.length);

            _Crypted += HASH[ANB];
            _Crypted += HASH[ANB2];
        }
        return _Crypted;
    }
	
	public static String CryptIP(String IP)
    {
		String[] Splitted = IP.split("\\.");
		String Encrypted = "";
        byte Count = 0;
        for (byte i = 0; i < 50; i++)
        {
            for (byte o = 0; o < 50; o++)
            {
                if (((i & 15) << 4 | o & 15) == Integer.parseInt(Splitted[Count]))
                {
                    Character A = (char)(i+48);
                    Character B = (char)(o + 48);
                    Encrypted += A.toString() + B.toString();
                    i = 0;
                    o = 0;
                    Count++;
                    if (Count == 4)
                        return Encrypted;
                }
            }
        }
        return "DD";
    }
	
	public static String CryptPort(short port)
	{
		char[] HASH = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
	            't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
	          'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'};
		short P = port;
		String nbr64 = "";
		for(byte a = 2;a>=0;a--)
		{
			nbr64 += HASH[(short)(P/(java.lang.Math.pow(64,a)))];
			P = (short)(P%(short)(java.lang.Math.pow(64,a)));
		}
		return nbr64;
	}
}
